﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Assesment.Library.DomainObject.Administration.Catalog;
using Assesment.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using Assesment.Web.Support;
using Assesment.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;


namespace Assesment.Web.Forms.Administration.Catalog
{

    public partial class ManagerialKeyBehaviourForm : BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {
            try
            {

                if (Request.Params["id"] != null)
                {
                    id = Convert.ToInt32(Request.Params["id"]);
                }


                DepartmentRepo DepartmentDal = new DepartmentRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);

                List<Department> departments = DepartmentDal.GetList();
                var query = departments.OrderBy(x => x.DepartmentDesc);
                departments = query.ToList();
                ddlDepartemen.Items.Clear();
                departments.ForEach(delegate(Department department)
                {
                    ddlDepartemen.Items.Add(new ListItem(department.DepartmentDesc, department.DeptID.ToString()));
                });

                ddlDepartemen.Items.Insert(0, new ListItem("-- pilih departemen --", string.Empty));


                LocationRepo LocationDal = new LocationRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
                List<Location> listlokasi = LocationDal.GetList();
                var querylokasi = listlokasi.OrderByDescending(x => x.LocationID);
                listlokasi = querylokasi.ToList();
                ddlLokasi.Items.Clear();
                listlokasi.ForEach(delegate(Location lokasi)
                {
                    ddlLokasi.Items.Add(new ListItem(lokasi.Name, lokasi.LocationID.ToString()));
                });

                ddlLokasi.Items.Insert(0, new ListItem("-- pilih lokasi --", string.Empty));


                Assesment.Library.DataAccess.Administration.Catalog.CompetencyRepo CompetencyDal = new Assesment.Library.DataAccess.Administration.Catalog.CompetencyRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                List<Competency> listCompetency = CompetencyDal.GetList();
                var queryCompetency = listCompetency.OrderBy(x => x.GroupCompetency);
                listCompetency = queryCompetency.ToList();
                ddlKompetensi.Items.Clear();
                listCompetency.ForEach(delegate(Competency compt)
                {
                    ddlKompetensi.Items.Add(new ListItem(compt.CompetncyDesc, compt.ComptID.ToString()));
                });

                ddlKompetensi.Items.Insert(0, new ListItem("-- pilih kompetensi --", string.Empty));


                if (!Page.IsPostBack)
                {
                    if (id > 0) // Edit an existing item
                    {
                        ManagerialKeyBehaviourRepo ManagerialKeyBehaviourDal = new ManagerialKeyBehaviourRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                        ManagerialKeyBehaviour obj = ManagerialKeyBehaviourDal.GetById(id);
                        if (obj != null)
                        {
                            hfID.Value = id.ToString();
                            //txtNama.Text = obj.NamaLevel;

                            PositionRepo PositionDal = new PositionRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
                            Position objPosition = PositionDal.GetById(Convert.ToInt32(obj.PositionID));
                            if (objPosition != null)
                            {
                                List<Position> listPosition = PositionDal.GetListByDepartmentID(Convert.ToInt32(objPosition.DepartmentID));
                                var queryPosition = listPosition.Where(x => x.LocationID == objPosition.LocationID);


                                listPosition = queryPosition.ToList();
                                ddlJabatan.Items.Clear();
                                listPosition.ForEach(delegate(Position position)
                                {
                                    ddlJabatan.Items.Add(new ListItem(position.PositionName, position.PositionID.ToString()));
                                });

                                ddlJabatan.Items.Insert(0, new ListItem("-- pilih jabatan --", string.Empty));

                                ddlJabatan.Items.FindByValue(obj.PositionID.ToString()).Selected = true;
                                ddlDepartemen.Items.FindByValue(objPosition.DepartmentID.ToString()).Selected = true;
                                ddlLokasi.Items.FindByValue(objPosition.LocationID.ToString()).Selected = true;

                            }

                            List<Competency> listComp = CompetencyDal.GetList();
                            var queryKomp = listComp.OrderBy(x => x.GroupCompetency);
                            listComp = queryKomp.ToList();
                            ddlKompetensi.Items.Clear();
                            listComp.ForEach(delegate(Competency objCompt)
                            {
                                ddlKompetensi.Items.Add(new ListItem(objCompt.CompetncyDesc, objCompt.ComptID.ToString()));
                            });

                            ddlKompetensi.Items.Insert(0, new ListItem("-- pilih kompetensi --", string.Empty));
                            ddlKompetensi.Items.FindByValue(obj.CompetencyID.ToString()).Selected = true;

                            Assesment.Library.DataAccess.Administration.Catalog.LevelCompetencyRepo LevelCompetencyDal = new Assesment.Library.DataAccess.Administration.Catalog.LevelCompetencyRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                            List<LevelCompetency> listLevel = LevelCompetencyDal.GetList();
                            var queryLevel = listLevel.Where(x => x.CompetencyID == obj.CompetencyID);
                            listLevel = queryLevel.ToList();
                            ddlLevel.Items.Clear();
                            listLevel.ForEach(delegate(LevelCompetency objLvl)
                            {
                                ddlLevel.Items.Add(new ListItem(objLvl.Level.ToString(), objLvl.ID.ToString()));
                            });

                            ddlLevel.Items.Insert(0, new ListItem("-- pilih level --", string.Empty));

                            ddlLevel.Items.FindByValue(obj.LevelCompetencyID.ToString()).Selected = true;

                        }

                        ddlDepartemen.Enabled = false;
                        ddlJabatan.Enabled = false;
                        ddlKompetensi.Enabled = false;
                        ddlLevel.Enabled = false;
                        ddlLokasi.Enabled = false;
                    }

                    GetPrivilege();


                }
            }
            catch (Exception ex) {
                ClientScript.RegisterClientScriptBlock(typeof(Page), "error", "alert('" +" Error :" +ex.Message + "');", true);
                return;
            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

    
    }

}
